    using UnityEngine;
using System.Collections;

public class newMenu : MonoBehaviour {

    private FadeDemo fade;
    public Transform lastRoom;
    public Transform currentRoom;
    public GameObject options;
    public Transform firstRoom;
    public GameObject achievements;
    public GameObject ManagerOptions;
    private GameObject lastScene;

    void Awake()
    {

        GameObject go = new GameObject();
        go.tag = "_Manager";
        go.name = "_Manager";
        go.AddComponent("FadeInScene");
    }

    void Start()
    {
        gameObject.AddComponent("FadeIn");
        gameObject.GetComponent<FadeIn>().createFade(1f);

        gameObject.GetComponent<CharacterMovement>().enabled = false;
        StartCoroutine(activePlayer());
        GameObject[] audio = GameObject.FindGameObjectsWithTag("AudioManager");

        if (audio.Length > 1)
        { 
            for (int i = 1; i < audio.Length; i++)
			{
                Destroy(audio[i]);
			}
        }

        fade = gameObject.AddComponent("FadeDemo") as FadeDemo;
    }

    public void Back()
    {
        StopAllCoroutines();
        StartCoroutine(activePlayer());
        lastScene.SetActive(false);
        iTween.MoveTo(gameObject, new Vector3(firstRoom.transform.position.x, firstRoom.transform.position.y, transform.position.z), 1);
    }

    public IEnumerator activePlayer()
    {
        yield return new WaitForSeconds(1);
        gameObject.GetComponent<CharacterMovement>().enabled = true;
    }

    void OnTriggerEnter(Collider obj)
    {
        if (obj.name == "Floor")
        {
            if (currentRoom != null)
            {
                if (obj.GetComponent<RoomInfo>().Father.name != currentRoom.name)
                {
                    if (obj.GetComponent<RoomInfo>().Father.name == "_Room_03_Options")
                    {
                        iTween.MoveTo(gameObject, new Vector3(obj.transform.position.x, obj.transform.position.y, transform.position.z), 1);
                        options.SetActive(true);
                        ManagerOptions.SendMessage("Resume");
                        fade.FadeOut(options.transform);
                        lastScene = options;
                        gameObject.GetComponent<CharacterMovement>().enabled = false;
                    }
                    if (obj.GetComponent<RoomInfo>().Father.name == "_Room_04_Achievements")
                    {
                        iTween.MoveTo(gameObject, new Vector3(obj.transform.position.x, obj.transform.position.y, transform.position.z), 1);
                        achievements.SetActive(true);
                        fade.FadeOut(achievements.transform);
                        lastScene = achievements;
                        achievements.SendMessage("ReturnInputCall", SendMessageOptions.DontRequireReceiver);
                        gameObject.GetComponent<CharacterMovement>().enabled = false;
                    }

                    lastRoom = currentRoom;
                    fade.FadeRoom(obj.GetComponent<RoomInfo>().Father, currentRoom);
                    currentRoom = obj.GetComponent<RoomInfo>().Father;
                }
            }

        }
    }

    void OnTriggerStay(Collider obj)
    {
        if (currentRoom == null)
        {
            currentRoom = obj.GetComponent<RoomInfo>().Father;
        }
    }

    void OnCollisionStay(Collision objectCollider)
    {
        GameObject obj = objectCollider.gameObject;

        if (obj.layer == 8)
        {
            if (obj.tag == "Door")
            {
                if (obj.name == "Exit")
                {
                    Application.Quit();
                }

                if (obj.GetComponent<LevelElements>())
                {
                    if (obj.GetComponent<LevelElements>().hasEnabled)
                    {
                        gameObject.AddComponent("SceneChange");

                        if (gameObject.GetComponent<SceneChange>().existFade())
                        {
                            string sceneName = "Level" + obj.GetComponent<LevelElements>().sceneName;
                            gameObject.GetComponent<SceneChange>().createFade(sceneName, 3);
                        }
                    }
                }
                else
                {

                    Destroy(obj.GetComponent<BoxCollider>());
                    obj.AddComponent("DoorDestroy");
                    obj.GetComponent<DoorDestroy>().amount = 1f;

                }
            }
        }
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.PageDown))
        {
            PlayerPrefs.DeleteAll();
        }
    }
}
